const date = new Date();
const nowYear = date.getFullYear();
const nowMonth = formatNumber(date.getMonth() + 1);
const nowDay = formatNumber(date.getDate());
// 每月的天数
let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// 根据年月 获取当月的总天数
function getDays(year, month) {
    if (month === 2) {
        return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 29 : 28;
    } else {
        return daysInMonth[month - 1];
    }
}
//获取两个日期之间的天数
function  getDaysBetween(dateString1,dateString2){
   var  startDate = Date.parse(dateString1);
   var  endDate = Date.parse(dateString2);
   var days=(endDate - startDate)/(1*24*60*60*1000);
    return  days;
}
 
// 根据年月日设置当前月有多少天 并更新年月日数组
function setDate(year, month, day, that) {
    let daysNum = year === nowYear && month === nowMonth ? nowDay : getDays(year, month);
    day = day > daysNum ? 1 : day;
    let monthsNum = year === nowYear ? nowMonth : 12;
    let years = [];
    let months = [];
    let days = [];
    let yearIdx = 0;
    let monthIdx = 0;
    let dayIdx = 0;
    // 重新设置年份列表
    for (let i = 1900; i <= nowYear; i++) {
        years.push(i);
    }
    years.map((v, idx) => {
        if (v === year) {
            yearIdx = idx;
        }
    });
    // 重新设置月份列表
    for (let i = 1; i <= monthsNum; i++) {
        months.push(formatNumber(i));
    }
    months.map((v, idx) => {
        if (v === month) {
            monthIdx = idx;
        }
    });
    // 重新设置日期列表
    for (let i = 1; i <= daysNum; i++) {
        days.push(formatNumber(i));
    }
    days.map((v, idx) => {
        if (v === day) {
            dayIdx = idx;
        }
    });

    that.setData({
        years, //年份列表
        months, //月份列表
        days, //日期列表
        year,
        month: formatNumber(month),
        day: formatNumber(day),
    });
    // 年月日数组有数据后，重新再设置一下vualue才能跳转到对应位置
    that.setData({
        value: [yearIdx, monthIdx, dayIdx],
    });
  
}
function getAfterTime(minate){
    var date = new Date();     //1. js获取当前时间
    var min = date.getMinutes();  //2. 获取当前分钟
    date.setMinutes(min + minate);  //3. 设置当前时间+10分钟：把当前分钟数+10后的值重新设置为date对象的分钟数
    var y = date.getFullYear();
    var m = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1);
    var d = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
    var h = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
    var f = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
    var s = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
    var formatdate = y + '-' + m + '-' + d + " " + h + ":" + f + ":" + s;
    console.log(formatdate) // 获取10分钟后的时间，格式为yyyy-mm-dd h:f:s
    return formatdate
}
function add0(m){return m<10?'0'+m:m }
function getAfterTimeBefore(){
    var timestamp = Date.parse(new Date());
    var a = 10800000;
    timestamp = timestamp - a;
    var time = new Date(timestamp);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
}

  /**
   * addTime 参赛时间
   * end 天数
   */
  function getNumberOfDays(addTime, end) {//获得天数
    if (addTime.indexOf('-') != -1) {
      addTime = addTime.replace(/-/g,"/")
    }
    if (addTime.indexOf('年') != -1) {
      addTime = addTime.replace("年", "/")
    }
    if (addTime.indexOf('月') != -1) {
      addTime = addTime.replace("月", "/")
    }
    if (addTime.indexOf('日') != -1) {
      addTime = addTime.replace("日", "")
    }
    ////////////////////////////////
    // if (end.indexOf('-') != -1) {
    //   end = end.replace("-", "/")
    // }
    // if (end.indexOf('年') != -1) {
    //   end = end.replace("年", "/")
    // }
    // if (end.indexOf('月') != -1) {
    //   end = end.replace("月", "/")
    // }
    // if (end.indexOf('日') != -1) {
    //   end = end.replace("日", "")
    // }
    console.log("end",end)
    console.log("addTime",addTime)

    //当前时间的时间戳
    var currentTimeStemp = new Date().getTime();
    console.log("currentTimeStemp",currentTimeStemp)

    //结束的时间戳
    var endTimeStemp = new Date(addTime).getTime() + parseInt(end) * 1000 * 60 * 60 * 24;
    console.log("endTimeStemp",endTimeStemp)

    //投票截至的时间戳
    let bettwen = endTimeStemp - currentTimeStemp;
    //时间戳转换成天数

    var day = parseInt(bettwen / 1000 / 60 / 60 / 24);//核心：时间戳相减，然后除以天数
    return day <= 0 ? 0 : day;
    // return day<0?"投票已结束":day;
  }
module.exports = {
    getNumberOfDays:getNumberOfDays,
    getAfterTime:getAfterTime,
    nowYear: nowYear,
    nowMonth: nowMonth,
    nowDay: nowDay,
    setDate: setDate,
    formatNumber: formatNumber,
    getAfterTimeBefore:getAfterTimeBefore,
	getDaysBetween
};
